// home模块的小仓库
import {
    reqCategoryList,
    reqGetBannerList,
    reqGetFloorList
} from '@/api'
//state 存储数据
const state = {
    categroryList: [],
    // 轮播图的数据
    bannerList: [],
    // 家用电器轮播图数据
    floorList: []
}

// mutations:修改state数据的唯一手段
const mutations = {
    CATEGORYLIST(state, categroryList) {
        state.categroryList = categroryList
    },
    GETBANNERLIST(state, bannerList) {
        state.bannerList = bannerList
        console.log("在修改仓库中的bannerList数据");
    },
    GETFLOORLIST(state, floorList) {
        state.floorList = floorList
    }
}

// actions: 处理自己的业务逻辑，也可以处理异步逻辑
const actions = {
    async categroryList({
        commit
    }) {
        let result = await reqCategoryList();
        if (result.code == 200) {
            commit('CATEGORYLIST', result.data.slice(0, 16))
        }
    },
    // 回去首页轮播图的数据
    async getBannerList({
        commit
    }) {
        console.log("在想服务器发送ajax请求，获取轮播图的数据");
        let result = await reqGetBannerList()
        if (result.code == 200) {
            commit('GETBANNERLIST', result.data)
        }
    },
    // 回去家用电器的数据
    async getFloorList({
        commit
    }) {
        let result = await reqGetFloorList()
        if (result.code == 200) {
            commit('GETFLOORLIST', result.data)

        }
        console.log(result);
    }
}

// getters:计算属性，简化仓库数据，让组件获取仓库的数据更加方便 
const getters = {}

export default {
    state,
    mutations,
    actions,
    getters,
}